<template>
  <div class="container">
    <h3>发表评论</h3>
    <hr>
    <textarea placeholder="来!请输入想要BB的内容(最多120字)" maxlength="120" v-model="message"></textarea>
    <mt-button type="primary" size="large" @click="postcommet">发表评论</mt-button>
    <div class="cmt-list">
      <div class="cmt-item" v-for="(item,index) in commentList" :key="item.add_time">
        <div
          class="cmt-title"
        >第{{(index+1)}}楼&nbsp;&nbsp;用户:{{item.userName}}&nbsp;&nbsp;发表时间:{{item.add_time}}</div>
        <div class="cmt-body">{{item.content}}</div>
      </div>
    </div>
    
    <mt-button type="danger" size="large" @click="getMore" plain>加载更多</mt-button>
  </div>
</template>

<script>
 //各种使用mock后台模拟
import api from "../../../aaa_lianxi/mock_comment.js";
import ap from "../../../aaa_lianxi/mock_post_comment.js";
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      message: "",
      indexPage: 1,
      commentList: [],
      postResult: {}
    };
  },
  props: ["id"],
  created() {
    this.getComment();
  },
  methods: {
    getComment() {
      this.$http.get("/news/comment").then(
        res => {
          if (res.status === 200) {
            this.commentList = this.commentList.concat(res.body.message);
          }
        },
        err => {
          Toast("获取评论失败");
        }
      );
    },
    getMore() {
      this.getComment();
    },
   
    postcommet() {
      if (this.message === ""||this.message.trim().length===0) {
        Toast({
          message: "评论内容不能为空",
          // position: "bottom"
        });
        return;
      } else {
        this.$http.post("/news/post_comment").then(
          res => {
            if (res.status === 200) {
              // console.log(res);
              this.postResult = res.body.result[0];
              Toast({
                message: this.postResult.message + "",
                position: "bottom"
              });
              this.message="";
            }
          },
          err => {
            Toast("上传评论失败");
            return;
          }
        );
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  h3 {
    font-size: 18px;
  }
  textarea {
    font-size: 14px;
    height: 85px;
    margin: 0;
  }
  .cmt-list {
    margin: 5px, 0px;
    .cmt-item {
      font-size: 13px;
      .cmt-title {
        line-height: 30px;
        margin-top: 2px;
        background-color: #ccc;
      }
      .cmt-body {
        line-height: 35px;
        text-indent: 2em;
      }
    }
  }
}
</style>
